package com.dark.sys.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dark.common.web.controller.BaseController;
import com.dark.common.web.model.DataGrid;
import com.dark.common.web.validate.AjaxResponse;
import com.dark.sys.service.MenuService;
import com.dark.sys.web.dto.MenuDto;

@Controller
@RequestMapping("/admin/sys/menu")
public class MenuController extends BaseController {

	@Autowired
	private MenuService menuService ;
	
	@RequiresRoles("admin")
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Integer id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public AjaxResponse add(MenuDto dto) {
		this.menuService.add(dto) ;
		return new AjaxResponse() ;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	public AjaxResponse delete(@PathVariable String id) {
		this.menuService.delete(id) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public AjaxResponse deleteBatch(@RequestParam String ids) {
		this.menuService.delete(ids) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public AjaxResponse update(MenuDto dto) {
		this.menuService.update(dto) ;
		return new AjaxResponse() ;
	}
	
	@ResponseBody
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	public MenuDto get(@PathVariable Integer id, Model model) {
		return this.menuService.get(id) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/datagrid")
	public DataGrid datagrid(MenuDto dto) {
		return this.menuService.datagrid(dto) ;
	}
	
	@ResponseBody
	@RequestMapping(value="/listAll")
	public List<MenuDto> listAll(MenuDto dto) {
		return this.menuService.listAll(dto) ;
	}
}
